﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using gfVehicleLibraryAJ;
using ConvertInfo;
using ProcessManage;
using AJNetLib;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace AJNetDll_XZSN
{
    public class WriteVehicle:IWriteVehicle
    {
        public ReturnInfo WriteByJKId(WriteCondition writeCondition)
        {
            ReturnInfo retInfo = new ReturnInfo();
            try
            {
                string strJKID = writeCondition.DicWritePara["jkid"];
                if (strJKID == "18C51")
                {//登录
                    return WriteVehicleInfoByJson(writeCondition.DicWritePara["C51PARA"],false);
                }
                else if (strJKID == "18C66")
                {//更新
                    Vehicle_18C66_Para m_18C66 = Get_18C66_FromDic(writeCondition.DicWritePara);
                    Vehicle_18C66_Result m_18C66_Result = Write_18C66(m_18C66);
                    retInfo.IsSucessful = m_18C66_Result.code == "1" ? true : false;
                    if (!retInfo.IsSucessful)
                    {
                        retInfo.Message = m_18C66_Result.message;
                    }
                }
                else if (strJKID == "18C65")
                {//复检
                    return WriteVehicleInfoByJson(writeCondition.DicWritePara["C51PARA"], true);
                }
                else if (strJKID == "18C72")
                {//退办
                    JObject C72Info = (JObject)JsonConvert.DeserializeObject(writeCondition.DicWritePara["C51PARA"]);
                    Vehicle_18C72_Para m_18C72 = new Vehicle_18C72_Para
                    {
                        xtlb = "18",
                        jkxlh = C72Info.Value<string>("jkxlh"),
                        jkid = "18C72",
                        jylsh = C72Info.Value<string>("jylsh"),
                        jyjgbh = C72Info.Value<string>("jyjgbh"),
                        hphm = C72Info.Value<string>("hphm"),
                        hpzl = C72Info.Value<string>("hpzl")
                    };
                    Vehicle_18C72_Result m_18C72_Result = Write_18C72(m_18C72);
                    retInfo.IsSucessful = m_18C72_Result.code == "1" ? true : false;
                    if (!retInfo.IsSucessful)
                    {
                        retInfo.Message = m_18C72_Result.message;
                    }
                }
                else if (strJKID == "18C54")
                {//路试结果录入
                    Vehicle_18C54_Para m_18C54 = Get_18C54_FromDic(writeCondition.DicWritePara);
                    Vehicle_18C54_Result m_18C54_Result = Write_18C54(m_18C54);
                    retInfo.IsSucessful = m_18C54_Result.code == "1" ? true : false;
                    if (!retInfo.IsSucessful)
                    {
                        retInfo.Message = m_18C54_Result.message;
                    }
                }
                else if (strJKID == "18C58")
                {
                    Vehicle_18C58_Para m_18C58 = Get_18C58_FromDic(writeCondition.DicWritePara);
                    Vehicle_18C58_Result m_18C58_Result = Write_18C58(m_18C58);
                    retInfo.IsSucessful = m_18C58_Result.code == "1" ? true : false;
                    if (!retInfo.IsSucessful)
                    {
                        retInfo.Message = m_18C58_Result.message;
                    }
                }
                else if (strJKID == "18C55")
                {
                    Vehicle_18C55_Para m_18C55 = Get_18C55_FromDic(writeCondition.DicWritePara);
                    m_18C55.jkid = "18C55";
                    Vehicle_18C55_Result m_18C55_Result = Write_18C55(m_18C55);
                    retInfo.IsSucessful = m_18C55_Result.code == "1" ? true : false;
                    if (!retInfo.IsSucessful)
                    {
                        retInfo.Message = m_18C55_Result.message;
                    }
                }
                else if (strJKID == "18C52")
                {//登录开始信息
                    string jstrLoginJson = writeCondition.DicWritePara["C51PARA"];
                    JObject JSON = (JObject)JsonConvert.DeserializeObject(jstrLoginJson);
                    Vehicle_18C52_Para c52 = new Vehicle_18C52_Para
                    {
                        jylsh = JSON.Value<string>("jylsh"),
                        jyjgbh = JSON.Value<string>("jyjgbh"),
                        jcxdh = "",
                        jkxlh = JSON.Value<string>("jkxlh"),
                        xtlb = JSON.Value<string>("xtlb"),
                        jkid = "18C52",
                        jycs = JSON.Value<int>("jycs"),
                        hpzl = JSON.Value<string>("hpzl"),
                        hphm = JSON.Value<string>("hphm"),
                        clsbdh = JSON.Value<string>("clsbdh"),
                        kssj = DateTime.Now
                    };                    
                    string message = "";
                    retInfo.IsSucessful = WriteStartTest(c52, ref message);
                    retInfo.Message = message;                    
                }
                else if (strJKID == "18S04")
                {
                    Dictionary<string,string> paradic = writeCondition.DicWritePara;
                    /* 照片抓拍 */
                    Vehicle_18J31_Para s04para = new Vehicle_18J31_Para
                    {
                        jkid = "18S04",
                        jkxlh = paradic["jkxlh"],
                        xtlb = "18",
                        jylsh = paradic["jylsh"],
                        jyjgbh = paradic["jyjgbh"],
                        hphm = paradic["hphm"],
                        hpzl = paradic["hpzl"],
                        clsbdh = paradic["clsbdh"],
                        jyxm = paradic["jyxm"],
                        zpzl = paradic["zpzl"],
                        jycs = int.Parse(paradic["jycs"]),
                        jcxdh = paradic["jcxdh"],
                    };
                    AJ_WriteProcess writeProcess = new AJ_WriteProcess();
                    Vehicle_18J31_Result s04result = writeProcess.AJ_Write_18J31(s04para);
                    retInfo.IsSucessful = s04result.code == "1";
                    if (!retInfo.IsSucessful)
                        retInfo.Message = s04result.message;
                }
            }
            catch (Exception ex)
            {
                retInfo.IsSucessful = false;
                retInfo.Message = ex.Message;
            }

            return retInfo;
        }

        /// <summary>
        /// IN:18C51 parameter entity JSON
        /// CALL 18S01,18C51,18C52 
        /// </summary>
        /// <param name="loginJson">18C51 parameter entity json,can convert to 18C51 parameter entity by deserilize this Json</param>
        /// <returns>lwlsh,jycs</returns>
        public ReturnInfo WriteVehicleInfoByJson(string loginJson,bool recheck)
        {
            //STEP 1 - 18S01 Get serial number 
            bool succ = false;
            JObject JSON = (JObject)JsonConvert.DeserializeObject(loginJson);
            Vehicle_18S01_Para s01 = new Vehicle_18S01_Para
            {
                jyjgbh = JSON.Value<string>("jyjgbh"),
                hphm = JSON.Value<string>("hphm"),
                hpzl = JSON.Value<string>("hpzl"),
                clsbdh = JSON.Value<string>("clsbdh"),
                xtlb=JSON.Value<string>("xtlb"),
                jkxlh=JSON.Value<string>("jkxlh"),
                jylb=JSON.Value<string>("jylb"),
                jkid="18S01",
                dlzd = "1",
                xzqhbm = ""
            };
            string lwlsh = null, jycs = null,message=null;
            succ = GetSrialNumber(s01, ref lwlsh, ref jycs,ref message);
            if (!succ) { return new ReturnInfo { IsSucessful = false, Message = "流水号获取失败:" + message }; }    
      
            //STEP 2 - 18C51/18C65 Write vehicle info 
            if (recheck)
            {
                Vehicle_18C65_Para c65 = JsonConvert.DeserializeObject<Vehicle_18C65_Para>(loginJson);
                c65.jycs = int.Parse(jycs);
                c65.jylsh = lwlsh;
                c65.jkid = "18C65";
                succ = WriteRecheckInfo(c65,ref message);
                if (!succ) { return new ReturnInfo { IsSucessful = false, Message = "复检信息写入失败:" + message }; }
            }
            else
            {
                Vehicle_18C51_Para c51 = JsonConvert.DeserializeObject<Vehicle_18C51_Para>(loginJson);
                c51.jkid = "18C51";
                c51.jylsh = lwlsh;
                c51.jycs = int.Parse(jycs);
                succ = WriteVehicleInfo(c51,ref message);
                if (!succ) { return new ReturnInfo { IsSucessful = false, Message = "写入登陆信息失败:" + message }; }
            }

            //STEP 3 - 18C52 Send start command
            Vehicle_18C52_Para c52 = new Vehicle_18C52_Para
            {
                jylsh = lwlsh,
                jyjgbh = JSON.Value<string>("jyjgbh"),
                jcxdh = "",
                jkxlh=JSON.Value<string>("jkxlh"),
                xtlb=JSON.Value<string>("xtlb"),
                jkid="18C52",
                jycs = int.Parse(jycs),
                hpzl = JSON.Value<string>("hpzl"),
                hphm = JSON.Value<string>("hphm"),
                clsbdh = JSON.Value<string>("clsbdh"),
                kssj = DateTime.Now
            };
            succ = WriteStartTest(c52,ref message);
            if (!succ) { return new ReturnInfo { IsSucessful = false, Message = "发送开始检测命令失败:" + message }; }
            
            //STEP 4 - Return lwlsh & jycs to Website
            Dictionary<string,string> netInfo = new Dictionary<string,string>();
            netInfo.Add("lwlsh",lwlsh);
            netInfo.Add("jycs",jycs);

            return new ReturnInfo
            {
                IsSucessful = true,
                Message = "命令写入成功，可以写入待检列表",
                DicRet = netInfo
            };
        }

        bool GetSrialNumber(Vehicle_18S01_Para s01,ref string lwlsh,ref string jycs,ref string msg)
        {
            AJ_ReadProcess write = new AJ_ReadProcess();
            Vehicle_18S01_Result result = write.AJ_Read_18S01(s01);            
            jycs = result.jycs;
            lwlsh = result.jylsh;
            msg = result.message;
            return result.code == "1";
        }

        bool WriteVehicleInfo(Vehicle_18C51_Para c51,ref string msg)
        {
            AJ_WriteProcess write = new AJ_WriteProcess();
            Vehicle_18C51_Result result = write.AJ_Write_18C51(c51);
            msg=result.message;
            return result.code == "1";
        }

        bool WriteStartTest(Vehicle_18C52_Para c52,ref string msg)
        {
            AJ_WriteProcess write = new AJ_WriteProcess();
            Vehicle_18C52_Result result = write.AJ_Write_18C52(c52);
            msg = result.message;
            return result.code == "1";
        }

        bool WriteRecheckInfo(Vehicle_18C65_Para c65,ref string msg)
        {
            AJ_WriteProcess write = new AJ_WriteProcess();
            Vehicle_18C65_Result result = write.AJ_Write_18C65(c65);
            msg = result.message;
            return result.code == "1";
        }

        #region 写入函数
        private Vehicle_18C51_Result Write_18C51(Vehicle_18C51_Para para_18C51)
        {
            AJ_WriteProcess writeProcess = new AJ_WriteProcess();
            Vehicle_18C51_Result result_18C51 = writeProcess.AJ_Write_18C51(para_18C51);
            return result_18C51;
        }

        private Vehicle_18C66_Result Write_18C66(Vehicle_18C66_Para para_18C66)
        {
            AJ_WriteProcess writeProcess = new AJ_WriteProcess();
            Vehicle_18C66_Result result_18C66 = writeProcess.AJ_Write_18C66(para_18C66);
            return result_18C66;
        }

        private Vehicle_18C65_Result Write_18C65(Vehicle_18C65_Para para_18C65)
        {
            AJ_WriteProcess writeProcess = new AJ_WriteProcess();
            Vehicle_18C65_Result result_18C65 = writeProcess.AJ_Write_18C65(para_18C65);
            return result_18C65;
        }

        private Vehicle_18C52_Result Write_18C52(Vehicle_18C52_Para para_18C52)
        {
            AJ_WriteProcess writeProcess = new AJ_WriteProcess();
            Vehicle_18C52_Result result_18C52 = writeProcess.AJ_Write_18C52(para_18C52);
            return result_18C52;
        }

        public Vehicle_18C55_Result Write_18C55(Vehicle_18C55_Para para_18C55)
        {
            AJ_WriteProcess writeProcess = new AJ_WriteProcess();
            Vehicle_18C55_Result result_18C55 = writeProcess.AJ_Write_18C55(para_18C55);
            return result_18C55;
        }

        private Vehicle_18C72_Result Write_18C72(Vehicle_18C72_Para para_18C72)
        {
            AJ_WriteProcess writeProcess = new AJ_WriteProcess();
            Vehicle_18C72_Result result_18C72 = writeProcess.AJ_Write_18C72(para_18C72);
            return result_18C72;
        }

        private Vehicle_18C54_Result Write_18C54(Vehicle_18C54_Para para_18C54)
        {
            AJ_WriteProcess writeProcess = new AJ_WriteProcess();
            Vehicle_18C54_Result result_18C54 = writeProcess.AJ_Write_18C54(para_18C54);
            return result_18C54;
        }

        public Vehicle_18C58_Result Write_18C58(Vehicle_18C58_Para para_18C58)
        {
            AJ_WriteProcess writeProcess = new AJ_WriteProcess();
            Vehicle_18C58_Result result_18C58 = writeProcess.AJ_Write_18C58(para_18C58);
            return result_18C58;
        }
        #endregion

        #region 得到字典集合
        /*主要接收页面发送过来的字典集合，字典集合中包括了很多字段，
         * 通过下面的函数，可以把字典中的字段转换成实体类，在对实体类进行操作
         * 如何集合中没有对应的字段，需要页面端进行添加
         */

        /// <summary>
        /// Dictionary change to 18C51 Para
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        private Vehicle_18C51_Para Get_18C51_FromDic(Dictionary<string, string> dic)
        {
            ModelReflection m_ModelReflection = new ModelReflection();
            Vehicle_18C51_Para m_18C51_Para = new Vehicle_18C51_Para();

            m_18C51_Para = m_ModelReflection.DictionaryToModel(m_18C51_Para, dic);

            return m_18C51_Para;
        }

        /// <summary>
        /// Dictionary change to 18C66 Para
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        private Vehicle_18C66_Para Get_18C66_FromDic(Dictionary<string, string> dic)
        {
            ModelReflection m_ModelReflection = new ModelReflection();
            Vehicle_18C66_Para m_18C66_Para = new Vehicle_18C66_Para();

            m_18C66_Para = m_ModelReflection.DictionaryToModel(m_18C66_Para, dic);

            return m_18C66_Para;
        }

        /// <summary>
        /// Dictionary change to 18C65 Para
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        private Vehicle_18C65_Para Get_18C65_FromDic(Dictionary<string, string> dic)
        {
            ModelReflection m_ModelReflection = new ModelReflection();
            Vehicle_18C65_Para m_18C65_Para = new Vehicle_18C65_Para();

            m_18C65_Para = m_ModelReflection.DictionaryToModel(m_18C65_Para, dic);

            return m_18C65_Para;
        }

        /// <summary>
        ///  Dictionary change to 18C52 Para
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        private Vehicle_18C52_Para Get_18C52_FromDic(Dictionary<string, string> dic)
        {
            ModelReflection m_ModelReflection = new ModelReflection();
            Vehicle_18C52_Para m_18C52_Para = new Vehicle_18C52_Para();

            m_18C52_Para = m_ModelReflection.DictionaryToModel(m_18C52_Para, dic);
            m_18C52_Para.kssj = DateTime.Now;

            return m_18C52_Para;
        }

        /// <summary>
        ///  Dictionary change to 18C55 Para
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        private Vehicle_18C55_Para Get_18C55_FromDic(Dictionary<string, string> dic)
        {
            ModelReflection m_ModelReflection = new ModelReflection();
            Vehicle_18C55_Para m_18C55_Para = new Vehicle_18C55_Para();

            m_18C55_Para = m_ModelReflection.DictionaryToModel(m_18C55_Para, dic);

            return m_18C55_Para;
        }
        

        /// <summary>
        ///  Dictionary change to 18C72 Para
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        private Vehicle_18C72_Para Get_18C72_FromDic(Dictionary<string, string> dic)
        {
            ModelReflection m_ModelReflection = new ModelReflection();
            Vehicle_18C72_Para m_18C72_Para = new Vehicle_18C72_Para();

            m_18C72_Para = m_ModelReflection.DictionaryToModel(m_18C72_Para, dic);

            return m_18C72_Para;
        }

        /// <summary>
        ///  Dictionary change to 18C54 Para
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        private Vehicle_18C54_Para Get_18C54_FromDic(Dictionary<string, string> dic)
        {
            ModelReflection m_ModelReflection = new ModelReflection();
            Vehicle_18C54_Para m_18C54_Para = new Vehicle_18C54_Para();

            m_18C54_Para = m_ModelReflection.DictionaryToModel(m_18C54_Para, dic);

            return m_18C54_Para;
        }


        /// <summary>
        ///  Dictionary change to 18C58 Para
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        private Vehicle_18C58_Para Get_18C58_FromDic(Dictionary<string, string> dic)
        {
            ModelReflection m_ModelReflection = new ModelReflection();
            Vehicle_18C58_Para m_18C58_Para = new Vehicle_18C58_Para();

            m_18C58_Para = m_ModelReflection.DictionaryToModel(m_18C58_Para, dic);

            return m_18C58_Para;
        }
        #endregion
    }
}
